package com.cy.pj.module.web;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice  //上面两个注解等价于这个注解
public class GlobalExceptionHandler {

    @ExceptionHandler(Throwable.class)
    public String doHandleThrowable(Throwable e){
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler.doHandleThrowable");
        //发邮件，发短信，报警（播放一端音乐）
        return "系统维护中";
    }

    @ExceptionHandler(RuntimeException.class)
    public String doHandleRuntimeException(RuntimeException e){
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler.doHandleRuntimeException");
        return "运行过程中出现了异常,异常信息为"+e.getMessage();
    }

    @ExceptionHandler(ArithmeticException.class)
    public String doHandleArithmeticException(ArithmeticException e){
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler");
        return "计算过程中出现了异常，异常信息为"+e.getMessage();
    }
}

